<template>
  <div id="globalHeader">
    <a-row :wrap="false">
      <a-col flex="200px">
        <div class="title-bar">
          <img class="logo" src="../assets/logo.svg" alt="logo" />
          <div class="title">vue用户中心</div>
        </div>
      </a-col>
      <a-col flex="auto">
        <a-menu
          v-model:selectedKeys="current"
          mode="horizontal"
          :items="items"
          @click="doMenuClick"
        />
      </a-col>
      <a-col flex="100px">
        <div class="user-login-status">
          <div v-if="loginUserStore.loginUser.username">
            {{loginUserStore.loginUser.username??"无名"}}
          </div>
          <div v-else>
            <a-button type="primary" href="/user/login">登录</a-button>
          </div>
        </div>
      </a-col>
    </a-row>
  </div>
</template>
<script lang="ts" setup>
import { h, ref } from 'vue'
import { HomeOutlined, CrownOutlined } from '@ant-design/icons-vue'
import type { MenuProps } from 'ant-design-vue'
import { useRouter } from 'vue-router'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'

const loginUserStore = useLoginUserStore();

const router = useRouter()
// 通过useRoute()这个钩子函数获取一个路由跳转器
const doMenuClick = ({ key }: { key: string }) => {
  console.log(key)
  router.push({
    path: key,
  })
}
//当前选中菜单
const current = ref<string[]>(['/'])
//监听路由变化,更新当前选中菜单
router.afterEach((to, from, failure) => {
  current.value = [to.path]
})

const items = ref<MenuProps['items']>([
  {
    key: '/',
    icon: () => h(HomeOutlined),
    label: '主页',
  },
  {
    key: '/user/login',
    label: '用户登录',
  },
  {
    key: '/user/register',
    label: '用户注册',
  },
  {
    key: '/admin/userManage',
    icon: () => h(CrownOutlined),
    label: '用户管理',
  },
  {
    key: '/others',
    label: h('a', { href: 'https://antdv.com', target: '_blank' }, '跳转到antd官网'),
  },
])
</script>
<style scoped>
.title-bar {
  display: flex;
  align-items: center;
}

.title {
  color: blue;
  font-size: 18px;
  margin-left: 20px;
}

.logo {
  height: 50px;
}
</style>
